from django.db import models
from django.utils import timezone

class SMSRecord(models.Model):
    PURPOSE_CHOICES = [
        ('change_phone', '更换手机号'),
        ('user_login', '用户登录'),
        ('reset_pwd', '密码重置')
    ]
    
    phone = models.CharField(max_length=11, db_index=True)
    purpose = models.CharField(max_length=20, choices=PURPOSE_CHOICES)
    code = models.CharField(max_length=6)
    session_id = models.CharField(max_length=64)  # 会话标识（用户ID或IP+UA哈希）
    is_used = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    expires_at = models.DateTimeField()

    class Meta:
        indexes = [
            models.Index(fields=['phone', 'purpose']),
            models.Index(fields=['session_id']),
        ]